import time

from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from head.Init_Driver import SurveyExaminationSystem_Driver


class MyPractice:
    url = ""
    driver = ""
    def __init__(self):
        # 集成测试
        self.url = "http://8.155.1.153:8081/exercise"
        self.driver = SurveyExaminationSystem_Driver.driver
        self.driver.get(self.url)
        self.driver.maximize_window()

        # 单元测试
        # self.url = "http://8.155.1.153:8081/user/login"
        # self.driver = SurveyExaminationSystem_Driver.driver
        # self.driver.get(self.url)
        # # 初始化登录成功状态
        # self.driver.find_element(By.ID, "username").send_keys("karen")
        # self.driver.find_element(By.ID, "password").send_keys("karen1234")
        # self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()
        # # 检查是否登录成功
        # # assert WebDriverWait(self.driver, 3).until(
        # #     expected_conditions.visibility_of_element_located((By.XPATH,"//a[h1='问卷系统']")))
        # assert WebDriverWait(self.driver, 3).until(
        #     expected_conditions.visibility_of_element_located(
        #         (By.CSS_SELECTOR,"#sk-layout > div > div > section > div > header > div > div.ant-pro-global-header-right-content > div > div > div")))
        # # 点击“我的练习”进入相关页面
        # self.driver.find_element(By.LINK_TEXT, "我的练习").click()

    def MyPracticeUIFuncTest(self):
        # 测试查询内容是否正常
        # 测试前截图，确保与测试后截图形成差别
        # assert WebDriverWait(self.driver, 3).until(
        #     expected_conditions.visibility_of_element_located(
        #         (By.CSS_SELECTOR,"span[class='ant-select-selection-search']")))
        SurveyExaminationSystem_Driver.Get_ScreenShot()
        # 测试下拉框存在并点击显示下拉内容
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"span[class='ant-select-selection-search']")))
        dropdown = self.driver.find_element(By.ID,"tempSave")
        dropdown.click()
        # 敲击键盘‘下’键，点两下方便截图内容前后更直观
        # dropdown.send_keys(Keys.ARROW_DOWN)
        dropdown.send_keys(Keys.ARROW_DOWN)
        # 敲击键盘‘确定’键
        dropdown.send_keys(Keys.ENTER)
        # 点击“查询”按钮
        self.driver.find_element(By.XPATH,"//span[text()='查 询']").click()
        time.sleep(2)
        # 测试后截图
        SurveyExaminationSystem_Driver.Get_ScreenShot()

        # 测试练习列表中“查看结果”按钮功能正常
        # 重新显示练习列表中的所有内容
        self.driver.find_element(By.XPATH,"//span[text()='重 置']").click()
        if not (self.driver.find_element(By.XPATH,"//div[text() = '暂无数据']")):
            assert WebDriverWait(self.driver,3).until(
                expected_conditions.visibility_of_element_located((By.LINK_TEXT,"继续答题")))
            self.driver.find_element(By.LINK_TEXT,"查看结果").click()
            # 检查是否跳转至相关页面
            assert WebDriverWait(self.driver,3).until(
                expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='在线练习']")))
            time.sleep(1)
            SurveyExaminationSystem_Driver.Get_ScreenShot()
            # 返回至“我的练习”页面
            self.driver.back()

        # 点击“我的练习”菜单栏中第三个按钮
        # assert WebDriverWait(self.driver,3).until(expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='测试']")))
        # self.driver.find_element(By.XPATH,"//span[text()='测试']").click()
        # # 在“查询框”中输入不存在的题库名称，测试查询功能是否正常
        # assert WebDriverWait(self.driver,3).until(expected_conditions.visibility_of_element_located((By.ID,"name")))
        # self.driver.find_element(By.ID,"name").send_keys("我不存在哦")
        # self.driver.find_element(By.XPATH,"//span[text()='查 询']").click()
        # time.sleep(1)
        # SurveyExaminationSystem_Driver.Get_ScreenShot()
        # # 点击“重置”按钮，测试功能是否正常
        # self.driver.find_element(By.XPATH,"//span[text()='重 置']").click()
        # # 点击题库列表中“开始练习”按钮
        # time.sleep(1)
        # self.driver.find_element(By.LINK_TEXT,"开始练习").click()
        # # 测试相关对话框是否弹出
        # assert WebDriverWait(self.driver,3).until(expected_conditions.visibility_of_element_located((By.XPATH,"//div[div='顺序练习']")))
        # self.driver.find_element(By.XPATH,"//div[div='顺序练习']").click()
        # # 检查是否跳转至相关页面
        # assert WebDriverWait(self.driver,3).until(expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='在线练习']")))
        # time.sleep(1)
        # SurveyExaminationSystem_Driver.Get_ScreenShot()
        # # 返回至“我的练习”页面
        # self.driver.back()
        #
        # # 点击“我的练习”菜单栏中第四个按钮
        # assert WebDriverWait(self.driver,3).until(expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='问题']")))
        # self.driver.find_element(By.XPATH,"//span[text()='问题']").click()
        # # 在“查询框”中输入不存在的题库名称，测试查询功能是否正常
        # assert WebDriverWait(self.driver,3).until(expected_conditions.visibility_of_element_located((By.ID,"name")))
        # self.driver.find_element(By.ID,"name").send_keys("我不存在哦")
        # self.driver.find_element(By.XPATH,"//span[text()='查 询']").click()
        # time.sleep(1)
        # SurveyExaminationSystem_Driver.Get_ScreenShot()
        # # 点击“重置”按钮，测试功能是否正常
        # self.driver.find_element(By.XPATH,"//span[text()='重 置']").click()
        # # 点击题库列表中“开始练习”按钮
        # time.sleep(1)
        # self.driver.find_element(By.LINK_TEXT,"开始练习").click()
        # # 测试相关对话框是否弹出
        # assert WebDriverWait(self.driver,3).until(expected_conditions.visibility_of_element_located((By.XPATH,"//div[div='顺序练习']")))
        # self.driver.find_element(By.XPATH,"//div[div='顺序练习']").click()
        # # 检查是否跳转至相关页面
        # assert WebDriverWait(self.driver,3).until(expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='在线练习']")))
        # time.sleep(1)
        # SurveyExaminationSystem_Driver.Get_ScreenShot()
        # # 返回至“我的练习”页面
        # self.driver.back()
        # time.sleep(3)


# myPractice = MyPractice()
# myPractice.MyPracticeUIFuncTest()
# myPractice.driver.quit()